<?php

// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------


namespace app\index\controller;

use app\index\BaseController;
use app\index\model\charge\ChargePileGun;
use app\index\model\charge\PileDayData;
use app\index\model\charge\PileMonthData;
use app\index\model\order\PileOrder;
use app\index\model\order\PileOrderCount;
use app\index\model\user\UserOrderRelation;
use app\index\model\user\UserPileRelation;
use app\index\model\user\UserDayData;
use app\index\model\user\UserMonthData;
use app\index\model\user\UserTimerOrder;
use app\index\model\user\User;
use Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use app\index\model\charge\ChargePile;


/**
 * Class Auth
 * @package app\controller\api
 * @author xaboy
 * @day 2020-05-06
 */
class Timer extends BaseController
{
    function timerSet(){

        //生成记录
        $getHouse = date("H:i");
        if($getHouse == '00:02'){
            //$this->userDayDataSet();
            //dump('生成记录');
        }
        if($getHouse == '00:10'){
            $this->virtuallyRecord();
            //dump('生成记录');
        }

        $getMin = date("i");
        //生成充电订单
        if($getMin % 2 == 0){
            $this->setTimerOrder();
            //dump('生成订单');
        }

        //订单拥有者设置
        if($getMin % 4 == 0){
            $this->setUserOrder();
            //dump('订单设置');
        }

        //统计订单
        if($getMin % 10 == 0){
            $this->pileDayOrderCount();
            $this->userDayOrderCount();
            //dump('订单统计');
        }

    }

    /**
     * 虚拟订单定时器记录生成
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function virtuallyRecord(){
        $limit = rand(6,10);
        $userTimerOrderModel  = new UserTimerOrder();
        $checkInfo = $userTimerOrderModel->getInfoByWhere(['day_data'=>date("Y-m-d")]);
        if(!$checkInfo){
            $userModel = new User();
            $addData = [];
            $userList = $userModel->where(['type'=>2])->limit($limit)->orderRaw('rand()')->select();
            foreach ($userList as $key=>$value){
                $addRation = rand(1,6);
                if($addRation <= 4){
                    $startTime = date("Y-m-d")." ".rand(0,9).":".rand(0,59).":".rand(0,59);
                }else{
                    $startTime = date("Y-m-d")." ".rand(0,23).":".rand(0,59).":".rand(0,59);
                }
                $timeHouseLog = rand(4,6);
                if($timeHouseLog == 6){
                    $timeLog = $timeHouseLog * 3600 + rand(0,16)*60 + rand(0,60);
                }else{
                    $timeLog = $timeHouseLog * 3600 + rand(0,60)*60 + rand(0,60);
                }
                $electricityData = rand(55,65) * 0.1;
                $chargeElectricity = bcmul(($timeLog/3600) , $electricityData ,2);
                $addData[] = [
                    'user_id'=>$value['id'],
                    'order_id'=>0,
                    'day_data'=>date("Y-m-d"),
                    'status'=>0,
                    'charge_electricity'=>$chargeElectricity,
                    'charge_money'=>0,
                    'charge_time'=>$timeLog,
                    'electricity_cost'=>0,
                    'service_cost'=>0,
                    'start_time'=>$startTime,
                    'pile_id'=>0,
                    'station_id'=>0,
                    'pile_gun_number'=>0,
                    'gun_id'=>0,
                    'create_time'=>date("Y-m-d H:i:s"),
                    'update_time'=>date("Y-m-d H:i:s"),
                ];
            }
            $result = $userTimerOrderModel->insertAll($addData);
        }
    }

    //操作虚拟订单记录
    function setTimerOrder(){
        $userTimerOrderModel  = new UserTimerOrder();
        $orderModel = new PileOrder();
        $userModel = new User();
        //添加虚拟订单
        $this->addVirtuallyOrder($userTimerOrderModel,$orderModel,$userModel);
        //设置完成的虚拟订单
        $this->completeOrder($userTimerOrderModel,$orderModel);

    }

    /**
     * 添加虚拟订单
     * @param $userTimerOrderModel
     * @param $orderModel
     * @param $userModel
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addVirtuallyOrder($userTimerOrderModel,$orderModel,$userModel){
        //获取未开始充电的虚拟记录
        $where = ['day_data'=>date("Y-m-d"),'status'=>0,'order_id'=>0];
        $getAddOrderList = $userTimerOrderModel->where($where)->where('start_time','<=',date("Y-m-d H:i:s"))->select();
        if($getAddOrderList){
            $getAddOrderList = $getAddOrderList->toArray();
            //dd($getAddOrderList);
            foreach ($getAddOrderList as $key=>$value){
                $userInfo = $userModel->getUserInfoById($value['user_id']);
                $getInfo = $this->getPileInfo();
                //dd($getInfo);
                if($getInfo['code'] == 0){
                    $nowTime = time();
                    $getHouse = date("H",$nowTime);
                    if(strtotime($value['start_time']) > (time() - 5*60)){
                        $startTime = $value['start_time'];
                    }else{
                        $startTime = date("Y-m-d H:i:s",$nowTime);
                    }
                    $endTime = date("Y-m-d H:i:s",(strtotime($startTime) + $value['charge_time']));
                    $moneyList = $this->getPrice(intval($getHouse));
                    $costInfo['cost_value'] = json_encode($moneyList,JSON_UNESCAPED_UNICODE);
                    $allServiceCost = round(($value['charge_electricity'] * $moneyList['service_cost']) ,2);
                    $allElectricityCost = round(($value['charge_electricity'] * $moneyList['electricity_cost']) , 2);
                    $timerData = [
                        'status'=>1,
                        'charge_money'=>$allServiceCost + $allElectricityCost,
                        'electricity_cost'=>$moneyList['electricity_cost'],
                        'service_cost'=>$moneyList['service_cost'],
                        'start_time'=>$startTime,
                        'end_time'=>$endTime,
                        'pile_id'=>$getInfo['pile_info']['id'],
                        'station_id'=>$getInfo['pile_info']['station_id'],
                        'gun_id'=>$getInfo['gun_info']['id'],
                        'pile_gun_number'=>$getInfo['gun_info']['num'],
                        'update_time'=>date("Y-m-d H:i:s"),
                        'cost_info'=>json_encode($moneyList,JSON_UNESCAPED_UNICODE),
                        'all_service_cost'=>$allServiceCost,
                        'all_charge_cost'=>$allElectricityCost,
                    ];
                    $addData = [
                        'start_time'=>$startTime,
                        'status'=>1,
                    ];
                    Db::startTrans();
                    $orderResult = $orderModel->addVirtuallyOrder($userInfo,$getInfo['pile_info'],$getInfo['gun_info'],$addData,$costInfo);
                    if($orderResult){
                        $timerData['order_id'] = $orderResult['id'];
                        $timerResult = $userTimerOrderModel->saveData($value['id'],$timerData);
                        if($timerResult){
                            Db::commit();
                        }else{
                            Db::rollback();
                        }
                    }else{
                        Db::rollback();
                    }
                }else{
                    break;
                }
            }
        }
    }

    //获取充电价格
    function getPrice($getHouse){
        $data = [
            0=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            1=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            2=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            3=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            4=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            5=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            6=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            7=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            8=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            9=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            10=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            11=>['electricity_cost'=>1.58,'service_cost'=>0.11],
            12=>['electricity_cost'=>1.58,'service_cost'=>0.11],
            13=>['electricity_cost'=>1.58,'service_cost'=>0.11],
            14=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            15=>['electricity_cost'=>1.58,'service_cost'=>0.11],
            16=>['electricity_cost'=>1.58,'service_cost'=>0.11],
            17=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            18=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            19=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            20=>['electricity_cost'=>1.27,'service_cost'=>0.16],
            21=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            22=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            23=>['electricity_cost'=>0.33,'service_cost'=>0.22],
            24=>['electricity_cost'=>0.33,'service_cost'=>0.22],
        ];
        if(isset($data[$getHouse])){
            return $data[$getHouse];
        }else{
            return ['electricity_cost'=>1.2,'service_cost'=>0.22];
        }
    }

    /**
     * 获取空闲充电设备
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getPileInfo(){
        $pileListWhere = ['is_delete'=>1,'is_use'=>1,'type'=>1];
        $pileIdList = Db::table("jy_user_pile_relation")->where($pileListWhere)->column('relation_id');
        //dump($pileIdList);
        //$where = ['p.station_id'=>1,'g.is_status'=>1];
        $where = ['g.is_status'=>1];
        $gunInfoList = Db::table('jy_charge_pile_gun')->alias('g')
            ->leftJoin("jy_charge_pile p",'g.pile_id = p.id')
            ->where($where)->whereIn('g.pile_id',$pileIdList)
            ->field('g.*')->select();
        //dd($gunInfoList);
        if($gunInfoList){
            $getInfo = [];
            $gunInfoList = $gunInfoList->toArray();
            shuffle($gunInfoList);
            foreach ($gunInfoList as $key=>$value){
                $getOrderInfo = Db::table('jy_pile_order')->where('gun_id',$value['id'])
                    ->where('status',1)->find();
                if(!$getOrderInfo){
                    $getInfo = $value;
                    break;
                }
            }
            if(empty($getInfo)) return ['code'=>1,'没有空闲的充电枪'];
            $pileInfo = Db::table('jy_charge_pile')->where('id',$getInfo['pile_id'])->find();
            if($pileInfo){
                return ['code'=>0,'pile_info'=>$pileInfo,'gun_info'=>$getInfo];
            }else{
                return ['code'=>2,'msg'=>'没有对应的充电桩'];
            }
        }else{
            return ['code'=>1,'没有对应的充电枪'];
        }
    }

    /**
     * 虚拟订单完结
     * @param $userTimerOrderModel
     * @param $orderModel
     */
    function completeOrder($userTimerOrderModel,$orderModel){
        $where = ['status'=>1];
        $getList = $userTimerOrderModel->where($where)->where('end_time','<=',date("Y-m-d H:i:s"))->where('order_id','>',0)->select();
        if($getList){
            $getList = $getList->toArray();
            foreach ($getList as $key=>$value){
                $timeData = ['status'=>2, 'update_time'=>date("Y-m-d H:i:s"),];
                $orderData = [
                    'charge_time'=>bcdiv($value['charge_time'],60,2),
                    'charge_number'=>$value['charge_electricity'],
                    'end_time'=>$value['end_time'],
                    'total_price'=>$value['charge_money'],
                    'pile_price'=>$value['charge_money'],
                    'pay_price'=>$value['charge_money'],
                    'paid'=>1,
                    'pay_time'=>date("Y-m-d H:i:s"),
                    'status'=>4,
                    'update_time'=>date("Y-m-d H:i:s"),
                    'all_service_cost'=>$value['all_service_cost'],
                    'all_charge_cost'=>$value['all_charge_cost'],
                ];
                $orderWhere = ['id'=>$value['order_id'],'order_type'=>2];
                Db::startTrans();
                $orderResult = $orderModel->where($orderWhere)->update($orderData);
                $timerResult = $userTimerOrderModel->saveData($value['id'],$timeData);
                if($orderResult && $timerResult){
                    Db::commit();
                }else{
                    Db::rollback();
                }
            }
        }

    }

    /**
     * 订单拥有者设置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function setUserOrder(){
        $orderWhere = ['is_del'=>0,'is_system_del'=>0,'order_user_id'=>0];
        $createTime = date("Y-m-d H:i:s",(time() - 30 * 24 * 60 * 60));
        $orderModel = new PileOrder();
        $userPileModel = new UserPileRelation();
        $orderList = $orderModel->where($orderWhere)->where('status','>',0)
            ->where('create_time','>=',$createTime)->limit(50)->select();
        if($orderList){
            $orderList = $orderList->toArray();
            $orderCountModel = new PileOrderCount();
            foreach ($orderList as $key=>$value){
                /*
                $where = ['relation_id'=>$value['pile_id'],'type'=>1,'is_delete'=>1,'is_use'=>1];
                $userPileInfo = $userPileModel->getInfoByWhere($where);
                if($userPileInfo && $userPileInfo['user_id'] > 0){
                    $orderUserId = $userPileInfo['user_id'];
                }else{
                    $orderUserId = -1;
                }
                */
                $orderUserId = -1;
                $saveData = ['order_user_id'=>$orderUserId,'update_time'=>date("Y-m-d H:i:s")];
                $orderCountStatus = $orderCountModel->getInfoByWhere(['order_id'=>$value['id']]);
                $countCountResult = true;
                Db::startTrans();
                $orderResult = $orderModel->updateOrder($value['id'],$saveData);
                if(!$orderCountStatus){
                    $countCountResult = $orderCountModel->insertGetId([
                        'order_id'=>$value['id'],
                        'user_id'=>$value['uid'],
                        'order_user_id'=>$orderUserId,
                        'is_count'=>0,
                        'create_time'=>date("Y-m-d H:i:s"),
                        'update_time'=>date("Y-m-d H:i:s"),
                    ]);
                }
                if($orderResult && $countCountResult){
                    Db::commit();
                }else{
                    Db::rollback();
                }
            }
        }
    }

    /**
     * 充电桩统计订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function pileDayOrderCount(){
        $orderCountModel = new PileOrderCount();
        $orderModel = new PileOrder();
        $where = ['c.is_count'=>0,'o.paid'=>1,'o.is_del'=>0,'o.is_system_del'=>0,'o.status'=>4];
        $countList = $orderCountModel->alias('c')
            ->leftJoin("jy_pile_order o",'c.order_id = o.id')
            ->where($where)->where('pay_time','>=',date("Y-m-d H:i:s",(time() - 50 * 24 * 60 * 60)))->limit(50)->field('c.*,o.paid,o.pay_time')->select();
        //dd($countList->toArray());
        if($countList){
            $countList = $countList->toArray();
            $pileDayDataModel = new PileDayData();
            $pileMonthDataModel = new PileMonthData();
            foreach ($countList as $key=>$value){
                $saveOrderData = [
                    'count_time'=>date("Y-m-d H:i:s"),
                    'update_time'=>date("Y-m-d H:i:s"),
                ];
                $orderInfo = $orderModel->getInfoByWhere(['id'=>$value['order_id']]);
                //没有对应的订单
                if(!$orderInfo){
                    $saveOrderData['is_count'] = -2;
                    $countResult = $orderCountModel->saveData($value['id'],$saveOrderData);
                }else{
                    $orderInfo = $orderInfo->toArray();
                    //进行统计，获取对应的记录
                    $pileDayData = $this->getPileDayData($orderInfo,$pileDayDataModel);
                    $pileMonthData = $this->getPileMonthData($orderInfo,$pileMonthDataModel);
                    if($pileDayData && $pileMonthData){
                        $saveOrderData['is_count'] = 1;
                        $saveDayData = [
                            'charge_electricity'=>$pileDayData['charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'],
                            'charge_money'=>$pileDayData['charge_money'] + $orderInfo['pay_price'],
                            'charge_number'=>$pileDayData['charge_number'] + 1,
                            'charge_time'=>$pileDayData['charge_time'] + bcdiv($orderInfo['charge_time'],60,2),
                            'service_cost'=>$pileDayData['service_cost'] + $orderInfo['all_service_cost'],
                            'charge_cost'=>$pileDayData['charge_cost'] + $orderInfo['all_charge_cost'],
                            'update_time'=>date("Y-m-d H:i:s"),
                        ];
                        $saveMonthData = [
                            'charge_electricity'=>$pileMonthData['charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'],
                            'charge_money'=>$pileMonthData['charge_money'] + $orderInfo['pay_price'],
                            'charge_number'=>$pileMonthData['charge_number'] + 1,
                            'charge_time'=>$pileMonthData['charge_time'] + bcdiv($orderInfo['charge_time'],60,2),
                            'service_cost'=>$pileMonthData['service_cost'] + $orderInfo['all_service_cost'],
                            'charge_cost'=>$pileMonthData['charge_cost'] + $orderInfo['all_charge_cost'],
                            'update_time'=>date("Y-m-d H:i:s"),
                        ];
                        //真实数据
                        if($orderInfo['order_type'] == 1){
                            //日数据
                            $saveDayData['real_charge_electricity'] = $pileDayData['real_charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'];
                            $saveDayData['real_charge_money'] = $pileDayData['real_charge_money'] + $orderInfo['pay_price'] ;
                            $saveDayData['real_charge_number'] = $pileDayData['real_charge_number'] + 1 ;
                            $saveDayData['real_charge_time'] = $pileDayData['real_charge_time'] + bcdiv($orderInfo['charge_time'],60,2);
                            $saveDayData['real_service_cost'] = $pileDayData['real_service_cost'] + $orderInfo['all_service_cost'] ;
                            $saveDayData['real_charge_cost'] = $pileDayData['real_charge_cost'] + $orderInfo['all_charge_cost'] ;
                            //月数据
                            $saveMonthData['real_charge_electricity'] = $pileMonthData['real_charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'];
                            $saveMonthData['real_charge_money'] = $pileMonthData['real_charge_money'] + $orderInfo['pay_price'] ;
                            $saveMonthData['real_charge_number'] = $pileMonthData['real_charge_number'] + 1 ;
                            $saveMonthData['real_charge_time'] = $pileMonthData['real_charge_time'] + bcdiv($orderInfo['charge_time'],60,2);
                            $saveMonthData['real_service_cost'] = $pileMonthData['real_service_cost'] + $orderInfo['all_service_cost'] ;
                            $saveMonthData['real_charge_cost'] = $pileMonthData['real_charge_cost'] + $orderInfo['all_charge_cost'] ;
                        }
                        Db::startTrans();
                        $countResult = $orderCountModel->saveData($value['id'],$saveOrderData);
                        $pileDayResult = $pileDayDataModel->saveData($pileDayData['id'],$saveDayData);
                        $pileMonthResult = $pileMonthDataModel->saveData($pileMonthData['id'],$saveMonthData);
                        if($countResult && $pileDayResult && $pileMonthResult){
                            Db::commit();
                        }else{
                            Db::rollback();
                            $countResult = $orderCountModel->saveData($value['id'],['update_time'=>date("Y-m-d H:i:s"),'remark'=>'更改记录失败']);
                        }
                    }else{
                        $countResult = $orderCountModel->saveData($value['id'],['update_time'=>date("Y-m-d H:i:s"),'remark'=>'缺少日记录/月份记录']);
                    }
                }
            }
        }
    }

    /**
     * 获取充电桩指定日期的记录
     * @param $orderInfo
     * @param $pileDayDataModel
     * @return mixed
     */
    function getPileDayData($orderInfo,$pileDayDataModel){
        $dayWhere = ['pile_id'=>$orderInfo['pile_id'],'day_data'=>date("Y-m-d",strtotime($orderInfo['pay_time']))];
        $pileDayInfo = $pileDayDataModel->getInfoByWhere($dayWhere);
        if($pileDayInfo){
            return $pileDayInfo;
        }else{
            return $pileDayDataModel->addData($orderInfo);
        }
    }

    /**
     * 获取充电桩指定月份的记录
     * @param $orderInfo
     * @param $pileMonthDataModel
     * @return mixed
     */
    function getPileMonthData($orderInfo,$pileMonthDataModel){
        $dayWhere = ['pile_id'=>$orderInfo['pile_id'],'day_month'=>date("Y-m",strtotime($orderInfo['pay_time']))];
        $pileMonthData = $pileMonthDataModel->getInfoByWhere($dayWhere);
        if($pileMonthData){
            return $pileMonthData;
        }else{
            return $pileMonthDataModel->addData($orderInfo);
        }
    }

    /**
     * 统计订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function userDayOrderCount(){
        $orderCountModel = new PileOrderCount();
        $orderModel = new PileOrder();
        $userRelationModel = new UserPileRelation();
        $where = ['c.user_count'=>0,'o.paid'=>1,'o.is_del'=>0,'o.is_system_del'=>0,'o.status'=>4];
        $countList = $orderCountModel->alias('c')
            ->leftJoin("jy_pile_order o",'c.order_id = o.id')
            ->where($where)->where('pay_time','>=',date("Y-m-d H:i:s",(time() - 50 * 24 * 60 * 60)))->limit(50)->field('c.*,o.paid,o.pay_time,o.pile_id')->select();
        //dd($countList->toArray());
        if($countList){
            $countList = $countList->toArray();
            $userDayDataModel = new UserDayData();
            $userMonthDataModel = new UserMonthData();
            $userOrderRelationModel = new UserOrderRelation();
            $relationWhere =  ['type'=>1,'is_use'=>1,'is_delete'=>1];
            foreach ($countList as $key=>$value){
                $saveOrderData = [
                    'update_time'=>date("Y-m-d H:i:s"),
                ];
                $userList = $userRelationModel->where(['relation_id'=>$value['pile_id']])->where($relationWhere)->select();
                //没有对应的获利人
                if(!$userList){
                    $saveOrderData['user_count'] = -1;
                    $countResult = $orderCountModel->saveData($value['id'],$saveOrderData);
                }else{
                    $userList = $userList->toArray();
                    //dd($userList);
                    $orderInfo = $orderModel->getInfoByWhere(['id'=>$value['order_id']]);
                    //dd($orderInfo->toArray());
                    //没有对应的订单
                    if(!$orderInfo){
                        $saveOrderData['user_count'] = -2;
                        $countResult = $orderCountModel->saveData($value['id'],$saveOrderData);
                    }else{
                        $orderInfo = $orderInfo->toArray();
                        //进行统计，获取对应的记录
                        $setDay = date("Y-m-d",strtotime($orderInfo['pay_time']));
                        $setMonth = date("Y-m",strtotime($orderInfo['pay_time']));
                        $checkDayResult = true;
                        $checkMonthResult = true;
                        foreach ($userList as $ks=>$vs){
                            $checkUserDayData = $this->getUserDayData($vs['user_id'],$setDay,$userDayDataModel);
                            $checkUserMonthData = $this->getUserMonthData($vs['user_id'],$setMonth,$userMonthDataModel);
                            $checkDayResult = $checkDayResult && $checkUserDayData;
                            $checkMonthResult = $checkMonthResult && $checkUserMonthData;
                        }
                        if($checkDayResult && $checkMonthResult){
                            $saveOrderData['user_count'] = 1;
                            $dayAllResult = true;
                            $monthAllResult = true;
                            $remark = '更改记录失败';
                            //dump($userList);
                            Db::startTrans();
                            foreach ($userList as $k=>$v){
                                if(strtotime($v['create_time']) < strtotime($orderInfo['pay_time'])){
                                    $userDayData = $this->getUserDayData($v['user_id'],$setDay,$userDayDataModel);
                                    $userMonthData = $this->getUserMonthData($v['user_id'],$setMonth,$userMonthDataModel);
                                    if($userDayData && $userMonthData){
                                        $saveDayData = [
                                            'charge_electricity'=>$userDayData['charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'],
                                            'charge_money'=>$userDayData['charge_money'] + $orderInfo['pay_price'],
                                            'charge_number'=>$userDayData['charge_number'] + 1,
                                            'charge_time'=>$userDayData['charge_time'] + bcdiv($orderInfo['charge_time'],60,2),
                                            'service_cost'=>$userDayData['service_cost'] + $orderInfo['all_service_cost'],
                                            'charge_cost'=>$userDayData['charge_cost'] + $orderInfo['all_charge_cost'],
                                            'update_time'=>date("Y-m-d H:i:s"),
                                        ];
                                        $saveMonthData = [
                                            'charge_electricity'=>$userMonthData['charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'],
                                            'charge_money'=>$userMonthData['charge_money'] + $orderInfo['pay_price'],
                                            'charge_number'=>$userMonthData['charge_number'] + 1,
                                            'charge_time'=>$userMonthData['charge_time'] + bcdiv($orderInfo['charge_time'],60,2),
                                            'service_cost'=>$userMonthData['service_cost'] + $orderInfo['all_service_cost'],
                                            'charge_cost'=>$userMonthData['charge_cost'] + $orderInfo['all_charge_cost'],
                                            'update_time'=>date("Y-m-d H:i:s"),
                                        ];
                                        //真实数据
                                        if($orderInfo['order_type'] == 1){
                                            //日数据
                                            $saveDayData['real_charge_electricity'] = $userDayData['real_charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'];
                                            $saveDayData['real_charge_money'] = $userDayData['real_charge_money'] + $orderInfo['pay_price'] ;
                                            $saveDayData['real_charge_number'] = $userDayData['real_charge_number'] + 1 ;
                                            $saveDayData['real_charge_time'] = $userDayData['real_charge_time'] + bcdiv($orderInfo['charge_time'],60,2);
                                            $saveDayData['real_service_cost'] = $userDayData['real_service_cost'] + $orderInfo['all_service_cost'] ;
                                            $saveDayData['real_charge_cost'] = $userDayData['real_charge_cost'] + $orderInfo['all_charge_cost'] ;
                                            //月数据
                                            $saveMonthData['real_charge_electricity'] = $userDayData['real_charge_electricity'] + $orderInfo['charge_number'] +  $orderInfo['loss_number'];
                                            $saveMonthData['real_charge_money'] = $userDayData['real_charge_money'] + $orderInfo['pay_price'] ;
                                            $saveMonthData['real_charge_number'] = $userDayData['real_charge_number'] + 1 ;
                                            $saveMonthData['real_charge_time'] = $userDayData['real_charge_time'] + bcdiv($orderInfo['charge_time'],60,2);
                                            $saveMonthData['real_service_cost'] = $userDayData['real_service_cost'] + $orderInfo['all_service_cost'] ;
                                            $saveMonthData['real_charge_cost'] = $userDayData['real_charge_cost'] + $orderInfo['all_charge_cost'] ;
                                        }
                                        $userDayResult = $userDayDataModel->saveData($userDayData['id'],$saveDayData);
                                        $userMonthResult = $userMonthDataModel->saveData($userMonthData['id'],$saveMonthData);
                                        $userOrderResult = $userOrderRelationModel->addRecord($v['user_id'],$orderInfo);
                                        $dayAllResult = $dayAllResult && $userDayResult && $userOrderResult;
                                        $monthAllResult = $monthAllResult && $userMonthResult;

                                    }else{
                                        $dayAllResult = false;
                                        $monthAllResult = false;
                                        $remark = '缺少对应的用户记录,用户id是：'.$v['user_id'];
                                    }
                                }else{
                                    //var_dump($v['user_id']);
                                }
                            }
                            $countResult = $orderCountModel->saveData($value['id'],$saveOrderData);
                            if($countResult && $dayAllResult && $monthAllResult){
                                Db::commit();
                            }else{
                                Db::rollback();
                                $countResult = $orderCountModel->saveData($value['id'],['update_time'=>date("Y-m-d H:i:s"),'remark'=>$remark]);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取用户指定日期的记录
     * @param $userId
     * @param $getDay
     * @param $userDayDataModel
     * @return mixed
     */
    function getUserDayData($userId,$getDay,$userDayDataModel){
        $dayWhere = ['user_id'=>$userId,'day_data'=>$getDay];
        $userDayInfo = $userDayDataModel->getInfoByWhere($dayWhere);
        if($userDayInfo){
            return $userDayInfo;
        }else{
            return $userDayDataModel->addUserData($userId,$getDay);
        }
    }

    /**
     * 获取用户指定月份的记录
     * @param $userId
     * @param $getMonth
     * @param $userMonthDataModel
     * @return mixed
     */
    function getUserMonthData($userId,$getMonth,$userMonthDataModel){
        $dayWhere = ['user_id'=>$userId,'day_month'=>$getMonth];
        $userMonthData = $userMonthDataModel->getInfoByWhere($dayWhere);
        if($userMonthData){
            return $userMonthData;
        }else{
            return $userMonthDataModel->addUserData($userId,$getMonth);
        }
    }

    /**
     * 统计指定设备每天的金额
     */
    function pileDayCount(){
        $pileIdList = [];
        $startTime = '2023-07-14';
        $endTime = '2023-07-17';
        $pileDayDataModel = new PileDayData();
        $orderModel = new PileOrder();
        $dayTime = $startTime;
        while (strtotime($dayTime) <= strtotime($endTime)){
            foreach ($pileIdList as $key=>$pileId){
                $pileDayInfo = $this->getPileDayInfo($pileId,$dayTime,$pileDayDataModel);
                $commonWhere = ['pile_id'=>$pileId,'paid'=>1,'is_del'=>0,'is_system_del'=>0,'status'=>4];
                $selectStart = $dayTime." 00:00:00";
                $selectEnd = $dayTime." 23:59:59";
                $checkCount = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)
                    ->where('pay_time','<=',$selectEnd)->count();
                if($checkCount > 0){
                    $chargeTime = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_time');
                    $realChargeTime = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where(['order_type'=>1])->sum('charge_time');
                    $saveOrder = [
                        'charge_electricity'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_number'),
                        'charge_money'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('pay_price'),
                        'charge_number'=>$checkCount,
                        'charge_time'=>bcdiv($chargeTime,60,2),
                        'service_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_service_cost'),
                        'charge_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_charge_cost'),
                        'real_charge_electricity'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('charge_number'),
                        'real_charge_money'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('pay_price'),
                        'real_charge_number'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->count(),
                        'real_charge_time'=>bcdiv($realChargeTime,60,2),
                        'real_service_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_service_cost'),
                        'real_charge_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_charge_cost'),
                        'remark'=>'按照充电桩进行统计',
                        'update_time'=>date("Y-m-d H:i:s"),
                    ];
                    $pileDayDataModel->saveData($pileDayInfo['id'],$saveOrder);
                }
            }
            $imtInfo = strtotime($dayTime) + 24 * 60 * 60;
            $dayTime = date("Y-m-d",$imtInfo);
        }
    }

    /**
     * 统计充电桩月份数据
     */
    /*
    function pileMonthCount(){
        $pileIdList = [];
        $monthList = ['2023-06','2023-07'];
        $pileMonthDataModel = new PileMonthData();
        $orderModel = new PileOrder();
        foreach ($monthList as $key=>$month){
            foreach ($pileIdList as $key=>$pileId){
                $pileDayInfo = $this->getPileMonthInfo($pileId,$month,$pileMonthDataModel);
                $commonWhere = ['pile_id'=>$pileId,'paid'=>1,'is_del'=>0,'is_system_del'=>0,'status'=>4];
                $selectStart = $month."-1 00:00:00";
                $selectEnd = date("Y-m-t",strtotime($selectStart))." 23:59:59";
                $checkCount = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)
                    ->where('pay_time','<=',$selectEnd)->count();
                if($checkCount > 0){
                    $chargeTime = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_time');
                    $realChargeTime = $orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where(['order_type'=>1])->sum('charge_time');
                    $saveOrder = [
                        'charge_electricity'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_number'),
                        'charge_money'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('pay_price'),
                        'charge_number'=>$checkCount,
                        'charge_time'=>bcdiv($chargeTime,60,2),
                        'service_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_service_cost'),
                        'charge_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_charge_cost'),
                        'real_charge_electricity'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('charge_number'),
                        'real_charge_money'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('pay_price'),
                        'real_charge_number'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->count(),
                        'real_charge_time'=>bcdiv($realChargeTime,60,2),
                        'real_service_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_service_cost'),
                        'real_charge_cost'=>$orderModel->where($commonWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_charge_cost'),
                        'remark'=>'按照充电桩进行统计',
                        'update_time'=>date("Y-m-d H:i:s"),
                    ];
                    $pileMonthDataModel->saveData($pileDayInfo['id'],$saveOrder);
                }
            }
        }
    }
    */

    /**
     * 统计充电桩月份数据
     */
    function pileMonthCount(){
        $pileIdList = [];
        $monthList = ['2023-06','2023-07'];
        $pileMonthDataModel = new PileMonthData();
        $pileDayDataModel = new PileDayData();
        $orderModel = new PileOrder();
        foreach ($monthList as $key=>$month){
            foreach ($pileIdList as $key=>$pileId){
                $pileDayInfo = $this->getPileMonthInfo($pileId,$month,$pileMonthDataModel);
                $dayWhere = ['pile_id'=>$pileId,'day_month'=>$month];
                $saveOrder = [
                    'charge_electricity'=>$pileDayDataModel->where($dayWhere)->sum('charge_electricity'),
                    'charge_money'=>$pileDayDataModel->where($dayWhere)->sum('charge_money'),
                    'charge_number'=>$pileDayDataModel->where($dayWhere)->sum('charge_number'),
                    'charge_time'=>$pileDayDataModel->where($dayWhere)->sum('charge_time'),
                    'service_cost'=>$pileDayDataModel->where($dayWhere)->sum('service_cost'),
                    'charge_cost'=>$pileDayDataModel->where($dayWhere)->sum('charge_cost'),
                    'real_charge_electricity'=>$pileDayDataModel->where($dayWhere)->sum('real_charge_electricity'),
                    'real_charge_money'=>$pileDayDataModel->where($dayWhere)->sum('real_charge_money'),
                    'real_charge_number'=>$pileDayDataModel->where($dayWhere)->sum('real_charge_number'),
                    'real_charge_time'=>$pileDayDataModel->where($dayWhere)->sum('real_charge_time'),
                    'real_service_cost'=>$pileDayDataModel->where($dayWhere)->sum('real_service_cost'),
                    'real_charge_cost'=>$pileDayDataModel->where($dayWhere)->sum('real_charge_cost'),
                    'remark'=>'按照充电桩进行统计',
                    'update_time'=>date("Y-m-d H:i:s"),
                ];
                $pileMonthDataModel->saveData($pileDayInfo['id'],$saveOrder);

            }
        }
    }

    /**
     * 获取充电桩指定日期的记录
     * @param $orderInfo
     * @param $pileDayDataModel
     * @return mixed
     */
    function getPileDayInfo($pileId,$getDay,$pileDayDataModel){
        $dayWhere = ['pile_id'=>$pileId,'day_data'=>$getDay];
        $pileDayInfo = $pileDayDataModel->getInfoByWhere($dayWhere);
        if($pileDayInfo){
            return $pileDayInfo;
        }else{
            return $pileDayDataModel->addData($pileId,$getDay);
        }
    }

    /**
     * 获取充电桩指定月份的记录
     * @param $orderInfo
     * @param $pileMonthDataModel
     * @return mixed
     */
    function getPileMonthInfo($pileId,$getMonth,$pileMonthDataModel){
        $dayWhere = ['pile_id'=>$pileId,'day_month'=>$getMonth];
        $pileMonthData = $pileMonthDataModel->getInfoByWhere($dayWhere);
        if($pileMonthData){
            return $pileMonthData;
        }else{
            return $pileMonthDataModel->addPileData($pileId,$getMonth);
        }
    }

    /**
     * 用户绑定充电桩获利统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function userDayProfitCount(){
        $userIdList = [];
        $userPileRelationModel = new UserPileRelation();
        $userDayDataModel = new UserDayData();
        $orderModel = new PileOrder();
        foreach ($userIdList as $key=>$userId){
            $startTime = '2023-07-14';
            $endTime = '2023-07-17';
            $relationWhere = ['user_id'=>$userId,'type'=>1,'is_use'=>1,'is_delete'=>1];
            $userRelationList = $userPileRelationModel->where($relationWhere)->select()->toArray();
            $dayTime = $startTime;
            //按天来统计
            while (strtotime($dayTime) <= strtotime($endTime)){
                $getUserDayInfo = $this->getUserDayData($userId,date("Y-m-d",$dayTime),$userDayDataModel);
                $saveOrder = $this->getZeroData();
                //循环用户每个充电桩
                foreach ($userRelationList as $k=>$relationInfo){
                    //生成的统计时间在统计时间之前
                    if(strtotime($relationInfo['count_time']) <= strtotime($dayTime." 23:59:59")){
                        $orderWhere = ['paid'=>1,'is_del'=>0,'is_system_del'=>0,'status'=>4,'pile_id'=>$relationInfo['relation_id']];
                        $selectStart = date("Y-m-d",$dayTime)." 00:00:00";
                        $selectEnd = date("Y-m-d",$dayTime)." 23:59:59";
                        $checkCount = $orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)
                            ->where('pay_time','<=',$selectEnd)->count();
                        if($checkCount > 0){
                            $chargeTime = $orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_time');
                            $realChargeTime = $orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where(['order_type'=>1])->sum('charge_time');
                            $saveOrder['charge_electricity'] = $saveOrder['charge_electricity'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('charge_number'));
                            $saveOrder['charge_money'] = $saveOrder['charge_money'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('pay_price'));
                            $saveOrder['charge_number'] = $saveOrder['charge_number'] + $checkCount;
                            $saveOrder['charge_time'] = $saveOrder['charge_time'] + bcdiv($chargeTime,60,2);
                            $saveOrder['service_cost'] = $saveOrder['service_cost'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_service_cost'));
                            $saveOrder['charge_cost'] = $saveOrder['charge_cost'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->sum('all_charge_cost'));
                            $saveOrder['real_charge_electricity'] = $saveOrder['real_charge_electricity'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('charge_number'));
                            $saveOrder['real_charge_money'] = $saveOrder['real_charge_money'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('pay_price'));
                            $saveOrder['real_charge_number'] = $saveOrder['real_charge_number'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->count());
                            $saveOrder['real_charge_time'] = $saveOrder['real_charge_time'] + bcdiv($realChargeTime,60,2);
                            $saveOrder['real_service_cost'] = $saveOrder['real_service_cost'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_service_cost'));
                            $saveOrder['real_charge_cost'] = $saveOrder['real_charge_cost'] + ($orderModel->where($orderWhere)->where('pay_time','>=',$selectStart)->where('pay_time','<=',$selectEnd)->where('order_type',1)->sum('all_charge_cost'));
                        }
                    }
                }
                //修改记录
                $userDayDataModel->saveData($getUserDayInfo['id'],$saveOrder);
                //下一天
                $imtInfo = strtotime($dayTime) + 24 * 60 * 60;
                $dayTime = date("Y-m-d",$imtInfo);
            }
        }
    }

    /**
     * 用户统计月份金额
     */
    function userMonthProfitCount(){
        $userIdList = [];
        $monthList = ['2023-06','2023-07'];
        $userDayDataModel = new UserDayData();
        $userMontDataModel = new UserMonthData();
        foreach ($userIdList as $key=>$userId){
            foreach ($monthList as $k=>$month){
                $getMonthDay = $this->getUserMonthData($userId,$month,$userMontDataModel);
                $dayWhere = ['user_id'=>$userId,'day_month'=>$month];
                $saveOrder = [
                    'charge_electricity'=>$userDayDataModel->where($dayWhere)->sum('charge_electricity'),
                    'charge_money'=>$userDayDataModel->where($dayWhere)->sum('charge_money'),
                    'charge_number'=>$userDayDataModel->where($dayWhere)->sum('charge_number'),
                    'charge_time'=>$userDayDataModel->where($dayWhere)->sum('charge_time'),
                    'service_cost'=>$userDayDataModel->where($dayWhere)->sum('service_cost'),
                    'charge_cost'=>$userDayDataModel->where($dayWhere)->sum('charge_cost'),
                    'real_charge_electricity'=>$userDayDataModel->where($dayWhere)->sum('real_charge_electricity'),
                    'real_charge_money'=>$userDayDataModel->where($dayWhere)->sum('real_charge_money'),
                    'real_charge_number'=>$userDayDataModel->where($dayWhere)->sum('real_charge_number'),
                    'real_charge_time'=>$userDayDataModel->where($dayWhere)->sum('real_charge_time'),
                    'real_service_cost'=>$userDayDataModel->where($dayWhere)->sum('real_service_cost'),
                    'real_charge_cost'=>$userDayDataModel->where($dayWhere)->sum('real_charge_cost'),
                    'remark'=>'按照月份进行统计',
                    'update_time'=>date("Y-m-d H:i:s"),
                ];
                //修改记录
                $userMontDataModel->saveData($getMonthDay['id'],$saveOrder);
            }
        }
    }

    /**
     * 设置参数为0
     * @param $getDayInfo
     * @return array
     */
    function setZeroData($getDayInfo){
        return [
            'charge_electricity'=>0,
            'charge_money'=>0,
            'charge_number'=>0,
            'charge_time'=>0,
            'service_cost'=>0,
            'charge_cost'=>0,
            'real_charge_electricity'=>0,
            'real_charge_money'=>0,
            'real_charge_number'=>0,
            'real_charge_time'=>0,
            'real_service_cost'=>0,
            'real_charge_cost'=>0,
            'remark'=>'按照日期进行统计',
            'update_time'=>date("Y-m-d H:i:s"),
        ];
    }

    /**
     * 定时器 增加充电桩
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function timeAddPile(){
        $dayInfo = date("d");
        if($dayInfo % 3 == 0){
            $where = ['is_virtually'=>2,'is_delete'=>1];
            $chargePileModel = new ChargePile();
            $pileGunModel = new ChargePileGun();
            $getDay = date("Y-m-d");
            $checkPile = $chargePileModel->where($where)->where('create_time','>=',$getDay." 00:00:00")
                ->where('create_time','<=',$getDay." 23:59:59")->find();
            //当天没有添加
            if(!$checkPile){
                $number = rand(3,5);
                //$number = 3;
                for($i = 0; $i<$number;$i++){
                    $getInfo = $this->getPileNumber($chargePileModel);
                    if($getInfo['code'] == 0){
                        $chargeType = rand(1,2);
                        Db::startTrans();
                        $pileId = $chargePileModel->insertGetId([
                            'title'=>$getInfo['data']['number']."#充电桩",
                            'device_sn'=>$getInfo['data']['device_sn'],
                            'station_id'=>5,
                            'number_sn'=>'01',
                            'device_type'=>rand(1,2),
                            'charge_type'=>$chargeType,
                            'power_number'=>$chargeType == 1 ? '28KW' : '7KW',
                            'voltage'=>'220V',
                            'electric_current'=>$chargeType == 1 ? '128A': '32A',
                            'charge_cost'=>1.27,
                            'service_cost'=>0.16000,
                            'is_status'=>1,
                            'is_use'=>1,
                            'is_delete'=>1,
                            'remark'=>'',
                            'create_time'=>date("Y-m-d H:i:s"),
                            'update_time'=>date("Y-m-d H:i:s"),
                            'peak_electric_cost'=>1.27000,
                            'peak_service_cost'=>0.16000,
                            'flat_electric_cost'=>0.33000,
                            'flat_service_cost'=>0.22000,
                            'valley_electric_cost'=>0.33000,
                            'valley_service_cost'=>0.22000,
                            'cost_id'=>1,
                            'cost_status'=>1,
                            'pile_sort'=>100,
                            'is_virtually'=>2,
                        ]);
                        if($pileId){
                            $gunResult = $pileGunModel->insertGetId([
                                'pile_id'=>$pileId,
                                'is_status'=>1,
                                'is_homing'=>1,
                                'num'=>'01',
                                'create_time'=>date("Y-m-d H:i:s"),
                                'update_time'=>date("Y-m-d H:i:s"),
                            ]);
                            if($gunResult){
                                Db::commit();
                            }else{
                                Db::rollback();
                            }
                        }else{
                            Db::rollback();
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取充电桩编号
     * @param $chargePileModel
     * @return array
     */
    function getPileNumber($chargePileModel){
        $count = $chargePileModel->where(['is_virtually'=>2,'is_delete'=>1])->count();
        $number = time().rand(1000,9999);
        $checkNumber = $chargePileModel->where(['is_delete'=>1])->where('device_sn',$number)->find();
        $backData = ['code'=>1,'data'=>''];
        if($checkNumber){
            $number = time().rand(1000,9999);
            $checkNumber = $chargePileModel->where(['is_delete'=>1])->where('device_sn',$number)->find();
            if(!$checkNumber) $backData = ['code'=>0,'data'=>['device_sn'=>$number,'number'=>$count]];
        }else{
            $backData = ['code'=>0,'data'=>['device_sn'=>$number,'number'=>($count + 1)]];
        }
        return $backData;
    }

    /*************************  绑定用户为充电站合伙人  **************************/
    //设置用户数据
    function setUserData(){
        dd(1);
        $userIdList = [20];
        //$this->setUserStore($userIdList);
        //$this->setUserBindPile($userIdList);
        //$this->setUserOrderRelation($userIdList);
        //$this->setUserDayData($userIdList);
        //$this->setUserMonthData($userIdList);
    }

    /**
     * 设置用户为合作关系
     * @param $userIdList
     */
    function setUserStore($userIdList){
        $userModel = new User();
        $result = $userModel->whereIn('id',$userIdList)->update(['store_type'=>2]);
    }

    /**
     * 绑定设备
     * @param $userIdList
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function setUserBindPile($userIdList){
        $userPileRelationModel = new UserPileRelation();
        $where = ['user_id'=>36];
        $recordList = $userPileRelationModel->where($where)->select();
        if($recordList){
            $recordList = $recordList->toArray();
            foreach ($recordList as $key=>$value){
                unset($recordList[$key]['id']);
            }
            foreach ($userIdList as $key=>$value){
                $insetData = [];
                $checkInfo = $userPileRelationModel->where(['user_id'=>$value])->find();
                if(!$checkInfo){
                    foreach ($recordList as $k=>$v){
                        $v['user_id'] = $value;
                        $insetData[] = $v;
                    }
                    //dd($insetData);
                    $addResult = $userPileRelationModel->insertAll($insetData);
                    if(!$addResult) dump($value."  插入失败---");
                }else{
                    dump($value."  已经存在记录---");
                }
            }
        }

    }


    /**
     * 设置订单关系
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function setUserOrderRelation($userIdList){
        $userOrderRelationModel = new UserOrderRelation();
        $where = ['user_id'=>36];
        $recordList = $userOrderRelationModel->where($where)->select()->toArray();
        foreach ($recordList as $key=>$value){
            unset($recordList[$key]['id']);
        }
        foreach ($userIdList as $key=>$value){
            $insetData = [];
            $checkInfo = $userOrderRelationModel->where(['user_id'=>$value])->find();
            if(!$checkInfo){
                foreach ($recordList as $k=>$v){
                    $v['user_id'] = $value;
                    $insetData[] = $v;
                }
                //dd($insetData);
                $addResult = $userOrderRelationModel->insertAll($insetData);
                if(!$addResult) dump($value."  插入失败---");
            }else{
                dump($value."  已经存在记录---");
            }
        }
    }

    /**
     * 设置用户日记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function setUserDayData($userIdList){
        $userDayDataModel = new UserDayData();
        $where = ['user_id'=>36];
        $recordList = $userDayDataModel->where($where)->select()->toArray();
        foreach ($recordList as $key=>$value){
            unset($recordList[$key]['id']);
        }
        foreach ($userIdList as $key=>$value){
            $insetData = [];
            $checkInfo = $userDayDataModel->where(['user_id'=>$value])->find();
            if(!$checkInfo){
                foreach ($recordList as $k=>$v){
                    $v['user_id'] = $value;
                    $insetData[] = $v;
                }
                //dd($insetData);
                $addResult = $userDayDataModel->insertAll($insetData);
                if(!$addResult) dump($value."  插入失败---");
            }else{
                dump($value."  已经存在记录---");
            }
        }
    }

    /**
     * 设置用户月度数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function setUserMonthData($userIdList){
        $userMonthDataModel = new UserMonthData();
        $where = ['user_id'=>36];
        $recordList = $userMonthDataModel->where($where)->select()->toArray();
        foreach ($recordList as $key=>$value){
            unset($recordList[$key]['id']);
        }
        foreach ($userIdList as $key=>$value){
            $insetData = [];
            $checkInfo = $userMonthDataModel->where(['user_id'=>$value])->find();
            if(!$checkInfo){
                foreach ($recordList as $k=>$v){
                    $v['user_id'] = $value;
                    $insetData[] = $v;
                }
                //dd($insetData);
                $addResult = $userMonthDataModel->insertAll($insetData);
                if(!$addResult) dump($value."  插入失败---");
            }else{
                dump($value."  已经存在记录---");
            }
        }
    }

}
